package com.ls.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.blog.dto.Recodes;
import com.ls.blog.entity.Links;
import com.ls.blog.mapper.LinksMapper;
import com.ls.blog.service.TLinksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.blog.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lishun
 * @since 2022-01-14
 */
@Service
public class TLinksServiceImpl extends ServiceImpl<LinksMapper, Links> implements TLinksService {

    @Resource
    private LinksMapper linksMapper;

    @Override
    public Recodes<Links> pageLinks(Long current, Long size) {
        Page<Links> tLinksPage = new Page<>(current,size);
        //构建条件
        QueryWrapper<Links> wrapper = new QueryWrapper<>();
        //以创建时间排序（降序）
        wrapper.orderByDesc("create_time");
        //调用mybatis plus分页方法进行查询
        IPage<Links> page = linksMapper.selectPage(tLinksPage, wrapper);
        return PageUtils.page(page);
    }

    @Override
    public List<Links> getLinksByName(String name) {
        return this.list(new LambdaQueryWrapper<Links>().like(Links::getBlogName,name));
    }

    @Override
    public void updateLink(Links links) {
        this.updateById(links);
    }

    @Override
    public void deleteLinkById(Long id) {
        this.removeById(id);
    }

    @Override
    public Links getLinksById(Long id) {
        return this.getById(id);
    }
}
